<?php
namespace Admin\Model;
use \Frame\Libs\BaseModel;

final class DonateModel extends BaseModel{
	public function fetchMoneyByPages($startrows,$pagesize){
		$sql = "SELECT * FROM love_donate_money LIMIT $startrows,$pagesize";
		return $this->pdo->fetchAll($sql);
	}
	
	public function fetchBookByPages($startrows,$pagesize){
		$sql = "SELECT * FROM love_donate_book LIMIT $startrows,$pagesize";
		return $this->pdo->fetchAll($sql);
	}
	
	//获取书籍捐赠记录数
	public function rowCountBook(){
		$sql = "SELECT donate_book_id FROM love_donate_book";
		return $this->pdo->rowCount($sql);
	}
	
	//获取资金捐赠记录数
	public function rowCountMoney(){
		$sql = "SELECT donate_money_id FROM love_donate_money";
		return $this->pdo->rowCount($sql);
	}
	
	//加载某一捐赠记录的详情列表
	public function getDonateBookDetailList($id){
		$sql = "SELECT * FROM love_donate_book_detail WHERE donate_book_id='$id'";
		return $this->pdo->fetchAll($sql);
	}
	
	//获取某一条捐书明细记录的信息
	public function getDonateBookDetail($id){
		$sql = "SELECT * FROM love_donate_book_detail WHERE donate_book_detail_id='$id'";
		return $this->pdo->fetchOne($sql);
	}
	
	//在love_book表计数isbn
	public function isExistBook($isbn){
		$sql = "SELECT book_id FROM love_book WHERE book_isbn='$isbn'";
		return $this->pdo->rowCount($sql);
	}
	
	//插入一条新纪录
	public function addNewBook($book_name,$book_isbn,$book_count){
		$sql = "INSERT INTO love_book (book_isbn,book_name,book_inventory) VALUES ('$book_isbn','$book_name','$book_count')";
		return $this->pdo->exec($sql);
	}
	
	//增加库存
	public function updateBookInventory($book_id,$book_count){
		$sql = "UPDATE love_book SET book_inventory=book_inventory+".$book_count." WHERE `book_id`='$book_id'";
		return $this->pdo->exec($sql);
	}
	
	//获取book_id
	public function getBookId($isbn){
		$sql = "SELECT book_id FROM love_book WHERE book_isbn='$isbn'";
		return $this->pdo->fetchOne($sql);
	}
	
	//设置status为已入库
	public function setDetailIn($id){
		$sql = "UPDATE love_donate_book_detail SET status=1 WHERE donate_book_detail_id='$id'";
		return $this->pdo->exec($sql);
	}
	
	//设置状态的几个方法
	public function setDonateBookReceived($id){
		$sql = "UPDATE love_donate_book SET status='1' WHERE donate_book_id='$id'";
		return $this->pdo->exec($sql);
	}
	public function setDonateBookIn($id){
		$sql = "UPDATE love_donate_book SET status='2' WHERE donate_book_id='$id'";
		return $this->pdo->exec($sql);
	}
	public function setDonateBookErr($id){
		$sql = "UPDATE love_donate_book SET status='3' WHERE donate_book_id='$id'";
		return $this->pdo->exec($sql);
	}
	public function setMoneyDonateCheck($id){
		$sql = "UPDATE love_donate_money SET status='1' WHERE donate_money_id='$id'";
		return $this->pdo->exec($sql);
	}
	public function setMoneyDonateErr($id){
		$sql = "UPDATE love_donate_money SET status='2' WHERE donate_money_id='$id'";
		return $this->pdo->exec($sql);
	}
	
	//查找某ID捐钱记录的捐钱金额
	public function fetchMoney($id){
	    $sql = "SELECT money FROM love_donate_money WHERE donate_money_id='$id'";
		return $this->pdo->fetchOne($sql);
	}
	
	//向common的money加
	public function addMoney($money){
	    $sql = "UPDATE love_common SET money=money+'$money' WHERE common_id='1'";
		return $this->pdo->exec($sql);
	}
}
?>